'''
BM8 链表中倒数最后k个结点
'''

class ListNode:
    def __init__(self,val,next = None):
        self.val = val
        self.next = next



class Solution:
    def FindKthToTail(self, pHead: ListNode, k: int) -> ListNode:
        if pHead == None:
            return None
        fast = pHead
        slow = pHead
        for i in range(0,k):
            if fast != None:
                fast = fast.next
            else:
                return None
        while fast:
            fast = fast.next
            slow = slow.next
        return slow

    def printListNode(self,node):
        while node:
            print(node.val,end='->' if node.next else "\n")
            node = node.next
if __name__ == "__main__":
    node1 = ListNode(1)
    node2 = ListNode(2)
    node3 = ListNode(4)
    node4 = ListNode(6)
    node5 = ListNode(3)
    node6 = ListNode(9)

    node1.next = node2
    node2.next = node3
    node3.next = node4
    node4.next = node5
    node5.next = node6

    solu = Solution()
    print("输入链表为")
    solu.printListNode(node1)
